package com.group1.ws;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.hibernate.Session;

import com.group1.model.Lecturer;
import com.group1.model.Question;
import com.group1.model.QuestionShortcut;
import com.group1.model.Topic;
import com.group1.util.SessionFactoryUtil;

@Path("/questionSC")
public class QuestionSCWS {
	
	
	@GET
	@Path("/getAll")
	@Produces(MediaType.TEXT_PLAIN)
	public String getAllQuestionSC(){
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		List<QuestionShortcut> list = QuestionShortcut.getAllQuestSc(session);
		String result;
		result = "{QUESTION_SC:[";
		boolean first = true;
		for (QuestionShortcut q : list) {
			Integer questSC_ID = q.getQuestshortId();
			Integer questID = q.getQuestion().getQuestionId();
			String topicID = q.getTopic().getTopicName();
			String questSC_Note = q.getQuestionshortNote();
			result += (first?"":",");
			result += "{";
			first = false;
			result += "\"questSC_ID\"" + ":\"" + questSC_ID + "\",";
			result += "\"questID\"" + ":\"" + questID + "\",";
			result += "\"topicID\"" + ":\"" + topicID + "\",";
			result += "\"questSC_Note\"" + ":\"" + questSC_Note + "\",";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/getByID/{questSC_ID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getQuestionSCByID(@PathParam("questSC_ID") String questSC_ID){
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		QuestionShortcut q = QuestionShortcut.getQuestScByID(Integer.valueOf(questSC_ID), session);
		String result;
		result = "{QUESTION_SC:{";
		result += "\"questSC_ID\"" + ":\"" + questSC_ID + "\",";
		result += "\"questID\"" + ":\"" + q.getQuestion().getQuestionId() + "\",";
		result += "\"topicID\"" + ":\"" + q.getTopic().getTopicName() + "\",";
		result += "\"questSC_Note\"" + ":\"" + q.getQuestionshortNote() + "\",";
		result += "}}";
		session.getTransaction().commit();
		return result;
	}
	@GET
	@Path("/getByTopic/{topicID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getQuestionSCByTopic(@PathParam("topicID") String topicID){
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		List<QuestionShortcut> list = QuestionShortcut.getQuestScByTopic(Integer.valueOf(topicID), session);
		String result;
		result = "{QUESTION_SC:[";
		boolean first = true;
		for (QuestionShortcut q : list) {
			Integer questSC_ID = q.getQuestshortId();
			Integer questID = q.getQuestion().getQuestionId();
			String topicName = q.getTopic().getTopicName();
			String questSC_Note = q.getQuestionshortNote();
			result += (first?"":",");
			result += "{";
			first = false;
			result += "\"questSC_ID\"" + ":\"" + questSC_ID + "\",";
			result += "\"questID\"" + ":\"" + questID + "\",";
			result += "\"topicID\"" + ":\"" + topicName + "\",";
			result += "\"questSC_Note\"" + ":\"" + questSC_Note + "\",";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	
	@GET
	@Path("/getByQuestion/{questID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getQuestionSCByQuestion(@PathParam("questID") String questID){
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		List<QuestionShortcut> list = QuestionShortcut.getQuestScByQuest(Integer.valueOf(questID), session);
		String result;
		result = "{QUESTION_SC:[";
		boolean first = true;
		for (QuestionShortcut q : list) {
			Integer questSC_ID = q.getQuestshortId();
			String topicID = q.getTopic().getTopicName();
			String questSC_Note = q.getQuestionshortNote();
			result += (first?"":",");
			result += "{";
			first = false;
			result += "\"questSC_ID\"" + ":\"" + questSC_ID + "\",";
			result += "\"questID\"" + ":\"" + questID + "\",";
			result += "\"topicID\"" + ":\"" + topicID + "\",";
			result += "\"questSC_Note\"" + ":\"" + questSC_Note + "\",";
			result += "}";
		}
		result += "]}";
		session.getTransaction().commit();
		return result;
	}
	
	@GET
	@Path("/create/{questID}/{topicID}/{questSC_Note}")
	@Produces(MediaType.TEXT_PLAIN)
	public String createQuestionSC(@PathParam("questID") String questID, @PathParam("topicID") String topicID, 
			@PathParam("questSC_Note") String questSC_Note)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 	QuestionShortcut q = new QuestionShortcut();
				q.setQuestion(Question.getQuestionByID(Integer.valueOf(questID), session));
				q.setTopic(Topic.getTopicByID(Integer.valueOf(topicID), session));
				q.setQuestionshortNote(questSC_Note);
				session.save(q);
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	
	@GET
	@Path("/update/{questSC_ID}/{questID}/{topicID}/{questSC_Note}")
	@Produces(MediaType.TEXT_PLAIN)
	public String updateQuestionSC(@PathParam("questSC_ID") String questSC_ID,@PathParam("questID") String questID,
			@PathParam("topicID") String topicID,@PathParam("questSC_Note") String questSC_Note)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 	QuestionShortcut q = QuestionShortcut.getQuestScByID(Integer.valueOf(questSC_ID), session);
			 	q.setQuestion(Question.getQuestionByID(Integer.valueOf(questID), session));
				q.setTopic(Topic.getTopicByID(Integer.valueOf(topicID), session));
				q.setQuestionshortNote(questSC_Note);
				session.saveOrUpdate(q);
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
	
	@GET
	@Path("/delete/{questSC_ID}")
	@Produces(MediaType.TEXT_PLAIN)
	public String deleteQuestionSC(@PathParam("questSC_ID") String questSC_ID)
	{
		Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 try {
			 session.delete(QuestionShortcut.getQuestScByID(Integer.valueOf(questSC_ID), session));
				session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return "error: " + e.getMessage();
		}
		 return "success";
	}
}
